/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.qb9.flashlib.profiling
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.system.System;
	import flash.utils.Timer;

	/**
	 * Simple text field that shows the Flash Player RAM memory in use.
	 */
	public class RAMMeter extends Meter
	{
		private var timer:Timer;

		/**
		 * Creates (autostarts and autopositions) a RAMMeter
		 */
		public function RAMMeter(interval:uint = 3000, x:Number = -60, y:Number = 35)
		{
			super(x, y);

			timer = new Timer(interval);
			
			init();
		}
		
		private function init():void
		{
			timer.addEventListener(TimerEvent.TIMER, update);
			timer.start();
			update();
		}

		private function format(mem:uint):String
		{
			return (mem / 1e6).toFixed(2) + ' mb';
		}

		private function update(e:Event = null):void
		{
			text = format(System.totalMemory);
		}

		override public function dispose():void
		{
			timer.removeEventListener(TimerEvent.TIMER, update);
			timer.stop();
			timer = null;
			super.dispose();
		}
	}
}
